package org.ecpkn.PAGridSim.core.Simulator;

import gridsim.AllocPolicy;
import gridsim.GridResource;
import gridsim.ResourceCalendar;
import gridsim.ResourceCharacteristics;

import java.util.Calendar;
import java.util.LinkedList;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ecpkn.PAGridSim.core.PAEntityFactory;
import org.ecpkn.PAGridSim.core.configuration.Configuration;
import org.ecpkn.PAGridSim.core.configuration.Configuration.Const;
import org.ecpkn.PAGridSim.core.gridsimEntity.paMachine.PAMachineList;
import org.ecpkn.PAGridSim.core.gridsimEntity.paResourceCharacteristics.PAResourceCharacteristics;
import org.ecpkn.PAGridSim.core.tasksCreation.PeriodTaskCreator;

public class ResourceCreater {
	private static final Logger logger = LogManager
			.getLogger(ResourceCreater.class);
	public static GridResource createGridResource(String name,
			int totalMachine, double timeZone, AllocPolicy policy)
			throws Exception {
		// Here are the steps needed to create a Grid resource:
		// 1. We need to create an object of MachineList to store machines
		PAMachineList mList = new PAMachineList();
		// TODO:a little strange
		for (int i = 0; i < totalMachine; i++) {
			// 2. Create one Machine with its id, number of PEs=1 and rating
			mList.add(PAEntityFactory.createCustomizedMachine(
					Configuration.getInstance().getParameterString(
							Const.SIMULATION_MACHINE), i, 1,
					PeriodTaskCreator.STANDARDRATING));
		}
		// 3. Create a ResourceCharacteristics object that stores the
		// properties of a Grid resource: architecture, OS, list of
		// Machines, allocation policy, time zone and its price
		String arch = "Sun Ultra"; // system architecture
		String os = "Solaris"; // operating system
		double cost = 3.0; // the cost of using this resource (G$/PE)��������

		// NOTE: allocation policy in here is set to
		// ResourceCharacteristics.TIME_SHARED????
		PAResourceCharacteristics resConfig = new PAResourceCharacteristics(
				arch, os, mList, ResourceCharacteristics.SPACE_SHARED,
				timeZone, cost);

		// 4. Finally, we need to create a GridResource object.
		double baud_rate = 1000.0; // communication speed��������
		long seed = 10L * 9 * 8 * 7 * 6 + 1;
		double peakLoad = 0.0; // the resource load during peak hour
		double offPeakLoad = 0.0; // the resource load during off-peak hr
		double holidayLoad = 0.0; // the resource load during holiday

		// incorporates weekends so the grid resource is on 7 days a week
		LinkedList<Integer> Weekends = new LinkedList<Integer>();
		Weekends.add(new Integer(Calendar.SATURDAY));
		Weekends.add(new Integer(Calendar.SUNDAY));

		// incorporates holidays. However, no holidays are set in this example
		LinkedList<Object> Holidays = new LinkedList<Object>();
		GridResource gridRes = null;

		// creates a resource calendar that handles different loads
		ResourceCalendar cal = new ResourceCalendar(
				resConfig.getResourceTimeZone(), peakLoad, offPeakLoad,
				holidayLoad, Weekends, Holidays, seed);

		try {
			// use this PA scheduling algorithm policy,The name of this entity
			// will be name_scheduler, e.g. Resource_scheduler.
			// then creates a grid resource entity.
			gridRes = new GridResource(name, baud_rate, resConfig, cal, policy);

		} catch (Exception e) {
			e.printStackTrace();
		}

		logger.debug("Creates one Grid resource with name = " + name);
		return gridRes;
	}
}
